package com.examsystem.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.examsystem.common.CookieUtils;
import com.examsystem.common.ResultJson;
import com.examsystem.common.StatusAndMsg;
import com.examsystem.entity.exame_user;
import com.examsystem.entity.exame_user_subject;
import com.examsystem.service.UserService;

/**
 * 用户管理
 * 
 * @author An-Il
 */
@RestController
@RequestMapping("/yhgl")
public class UserController {

	@Autowired
	private UserService useService;

	// 查询用户（分页）
	@PostMapping("/selectUserList")
	public ResultJson selectUserList(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer pageSize) {
		ResultJson result = null;
		result = useService.selectUserList(page, pageSize);
		return result;
	}

	// 模糊查询用户（分页）
	@PostMapping("/selectUserByExample")
	public ResultJson selectUserByExample(exame_user user, @RequestParam(defaultValue = "DESC") String Sort,
			@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
		ResultJson result = null;
		result = useService.selectUserByExample(user, Sort, page, pageSize);
		return result;
	}

	// 根据id修改用户信息
	@PostMapping("/updateByKey")
	public ResultJson updateByKey(exame_user user) {
		ResultJson result = null;
		result = useService.updateByKey(user);
		return result;
	}

	/**
	 * 根据id删除用户信息（支持批量）
	 * @param ids
	 * 批量：1-2-3
	 * 单个：1
	 * @return
	 */

	@PostMapping("/deleteBykey")
	public ResultJson deleteBykey(String ids) {
		ResultJson result = null;
		if(StringUtils.isBlank(ids)) {
			result=new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
		}else {
			//批量删除
			List<String> del_ids = new ArrayList<>();
			if(ids.contains("-")) {
				String[] str_ids = ids.split("-");
				//组装id的集合
				for (String string : str_ids) {
					del_ids.add(string);
				}
			}else {
				del_ids.add(ids);
			}
			result = useService.deleteBykey(del_ids);
		}
		return result;
	}
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	@PostMapping("/addUser")
	public ResultJson addUser(exame_user user) {
		ResultJson result=null;
		//判断
		if(StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword())||
				StringUtils.isBlank(user.getRealname())||StringUtils.isBlank(user.getType())) {
			result=new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), "参数错误");
		}else {
			result = useService.addUser(user);
		}
		return result;
	}

	/**
	 * 用户登录
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@PostMapping("/login")
	public ResultJson login(exame_user user, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		ResultJson result = null;
		// 验证
		if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
			result = new ResultJson(StatusAndMsg.LOGINPARAMERROR.getStatus(), StatusAndMsg.LOGINPARAMERROR.getMsg(),null);
		} else {
			// 调用service
			result = useService.selectByUserAndPwd(user);
			if (result.getStatus() == 202) {
				// 登录成功，存session，存cookie
				session.setAttribute("userInfo", result.getData());
				CookieUtils.setCookie(request, response, "userInfo", JSON.toJSONString(result.getData()), true);
			}
		}
		// 返回json数据
		return result;
	}
	
	/**
	 * 用户与科目关联（学生，老师）
	 * @param us
	 * @return
	 */
	@PostMapping("/user_subject")
	public ResultJson user_subject(String username,String stids) {
		ResultJson result=null;
		//判断
		if(StringUtils.isBlank(username)||StringUtils.isBlank(stids)) {
			result = new ResultJson(StatusAndMsg.LOGINPARAMERROR.getStatus(), StatusAndMsg.LOGINPARAMERROR.getMsg(),null);
		}else {
			//批量关联
			List<Integer> del_ids = new ArrayList<>();
			if(stids.contains("-")) {
				String[] str_ids = stids.split("-");
				//组装id的集合
				for (String string : str_ids) {
					del_ids.add(Integer.parseInt(string));
				}
				result=useService.user_subject(username,del_ids);
			}else {
				result = new ResultJson(StatusAndMsg.LOGINPARAMERROR.getStatus(), StatusAndMsg.LOGINPARAMERROR.getMsg(),null);
			}
		}
		return result;
	}

}
